/*
 * 上海远境文化传媒有限公司 版权所有
 */
package facade.open.response;

import facade.base.CorpFacade;
import helper.DateTimeFormater;
import helper.UrlHelper;
import models.base.Corp;
import models.enums.LangCode;
import play.i18n.Messages;

import java.io.Serializable;
import java.util.List;

/**
 * 公司信息Response.
 */
public class CorpResponse extends ResponseWithAbility implements Serializable {

    private static final long serialVersionUID = -9031875317573889245L;

    public String id;

    /**
     * 公司名.
     */
    public String name;

    /**
     * 根公司ID.
     */
    public String rootCorpId;

    public List<AdminUserResponse> admins;

    public List<String> func;

    /**
     * 到期时间.
     */
    public String expiredAt;

    /**
     * 授权版本.
     */
    public String licenseEdition;


    /**
     * 公司邮箱后缀，@部分的内容，全小写；多个时用小写逗号分隔
     *
     * 如： safeguard.com
     */
    public String emailPostfix;

    /**
     * 公司logo的Ali OSS Object名字.
     * 方形logo.
     */
    public String logoUrl;

    /**
     * 公司通告.
     */
    public String notice;

    /**
     * 公司级别的语言.
     */
    public LangCode lang;

    /**
     * 公司域名.
     */
    public String customCorpUrl;
    public String customCorpDomain;

    public static CorpResponse from(Corp corp) {
        if (corp == null) {
            return null;
        }
        CorpResponse corpResponse = new CorpResponse();
        corpResponse.id = corp.id;
        corpResponse.name = corp.name;
        if (!corp.id.equals(corp.rootCorpId)) {
            corpResponse.rootCorpId = corp.rootCorpId;
        }

        return corpResponse;
    }

    public static CorpResponse fullFrom(Corp corp) {
        CorpResponse corpResponse = from(corp);
        if (corp == null) {
            return null;
        }
        corpResponse.expiredAt = DateTimeFormater.date(corp.expiredAt);
        corpResponse.licenseEdition = Messages.get("licenseEdition." + corp.licenseEdition);
        corpResponse.lang = corp.lang;
        corpResponse.emailPostfix = corp.emailPostfix;
        corpResponse.logoUrl = UrlHelper.getUploadOssImageUrl(corp.getSafeLogoObject());
        corpResponse.notice = corp.notice;

        corpResponse.customCorpUrl = corp.getDefaultUrl();
        corpResponse.customCorpDomain = corp.getDefaultUrl();
        return corpResponse;
    }

    /**
     * 加扩展信息（授权等.）
     * @param corp
     * @return
     */
    public static CorpResponse fullFromWithExtInfo(Corp corp) {
        CorpResponse corpResponse = fullFrom(corp);
        if (corp == null) {
            return null;
        }

        corpResponse.func = CorpFacade.getCorpFuncs(corp);

        return corpResponse;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("CorpResponse{");
        sb.append("id='").append(id).append('\'');
        sb.append(", name='").append(name).append('\'');
        sb.append('}');
        return sb.toString();
    }
}
